﻿using System.Collections;
using System.Collections.Generic;
namespace YanZhiwei.DotNet2.Utilities.Base
{
    /// <summary>
    /// IEnumerable的线程安全的实现帮助类
    /// 参考: http://www.codeproject.com/KB/cs/safe_enumerable.aspx
    /// </summary>
    public class ThreadSafeEnumerableHelper<T> : IEnumerable<T>
    {
        #region 构造函数以及变量
        private readonly IEnumerable<T> _inner;
        private readonly object _lock;
        /// <summary>
        /// ThreadSafeEnumerable构造函数
        /// </summary>
        /// <param name="inner">IEnumerable</param>
        /// <param name="lock">object</param>
        public ThreadSafeEnumerableHelper(IEnumerable<T> inner, object @lock)
        {
            _lock = @lock;
            _inner = inner;
        }
        #endregion
        #region GetEnumerator
        /// <summary>
        /// GetEnumerator
        /// </summary>
        /// <returns>IEnumerator</returns>
        public IEnumerator<T> GetEnumerator()
        {
            return new ThreadSafeEnumerator<T>(_inner.GetEnumerator(), _lock);
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion 
    }
}